package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.Rf;
import cn.com.utt.unms.pojo.CustomRf;
import cn.com.utt.unms.pojo.Device;
import cn.com.utt.unms.service.RfService;
import cn.com.utt.unms.service.CustomRfService;
import cn.com.utt.unms.service.DeviceService;
import cn.com.utt.unms.vo.RfInput;

@RestController
@RequestMapping("v1/rfs")
public class RfController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;
	
	@Autowired
	RfService rfService;
	
	@Autowired
	DeviceService deviceService;
	
	@Autowired 
	CustomRfService customRfService;
	
	/**
	 * 查询当前用户的射频列表
	 * @param principal
	 * @return
	 */
	@RequestMapping(value="selectList",method={RequestMethod.POST})
	public Map<String,Object> selectList(Principal principal,@RequestParam("deviceId[]")List<Long> deviceIds){
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		List<Rf> rfs=rfService.queryRfsByUserID(user.getParentId() == null?user.getId():user.getParentId());
		CustomRf customRf=null;
		if(deviceIds.size()<=1){
			customRf=customRfService.queryByDeviceID(deviceIds.get(0));
		}
		result.put("rfs", rfs);
		result.put("customRf", customRf);
		return result.toJsonObject();
	}
	/**
	 * ap详情设备射频配置下发
	 * @param principal
	 * @param configRf
	 * @return
	 */
	@PreAuthorize("hasRole('DEVICE_EDIT')")
	@RequestMapping(value="saverf",method={RequestMethod.POST},consumes = "application/json")
	public Map<String,Object> updateConfigRf(Principal principal,@RequestBody RfInput rfInput) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		result=rfService.save(user.getParentId() == null?user.getId():user.getParentId(), rfInput);
		return result.toJsonObject();
	}
	/**
	 * 射频模板配置
	 * @param principal
	 * @param configRf
	 * @return
	 */
	@PreAuthorize("hasRole('CONFIG_EDIT')")
	@RequestMapping(value="editRf",method={RequestMethod.POST},consumes = "application/json")
	public Map<String,Object> editConfigRf(Principal principal,@RequestBody Rf rf){
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		result=rfService.edit(user.getParentId() == null?user.getId():user.getParentId(),rf);
		return result.toJsonObject();
		
	}
	@PreAuthorize("hasRole('CONFIG_VIEW')")
	@RequestMapping(value = "/search", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public Map<String, Object> list(@RequestBody TableInput input, Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		TableOutput<Rf> output = rfService.queryByCondition(input,user.getParentId() == null?user.getId():user.getParentId());
		RestResult result = new RestResult();
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("configRfs", output.getList());
		if (user.hasRole("ROLE_CONFIG_EDIT")) {
			result.put("hasEditRole", true);
		} else {
			result.put("hasEditRole", false);
		}
		return result.toJsonObject();
	}
	
	@PreAuthorize("hasRole('CONFIG_EDIT')")
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> delete(@RequestParam(value = "configRfIds[]")long[] configRfIds,Principal principal) {
		RestResult result;
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		result = rfService.delete(configRfIds,user.getParentId() == null?user.getId():user.getParentId());
		return result.toJsonObject();
	}
	/**
	 * router详情设备配置批量下发
	 * @return
	 */
	@PreAuthorize("hasRole('DEVICE_EDIT')")
	@RequestMapping(value="batchUpdateRouter",method={RequestMethod.POST})
	public Map<String,Object> updateRouterRf(Long deviceId,String jsonStr,String oper,Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		Device device = deviceService.queryDeviceById(deviceId);
		if (device == null || !deviceService.hasRight(device, user.getGroupId())) {
			result.setErrorCode(-1);
			result.setErrorMsg(messageSourceUtil.getMessage(Tips.PermissionDenied));
			return result.toJsonObject();
		}
		result=rfService.updateRouter(device,jsonStr,oper);
		return result.toJsonObject();
	}
	
}
